﻿<?php
require_once('../db/check_login.php');
require_once('../db/config.php');
$db=new db($config);
$res=$db->foundbyWhere('role');
?>
<p><h2>添加用户</h2></p>
<form name="form1" method="post" action="add.php">
  <table width="100%" border="0">
    <tr>
      <td width="15%">用户名:</td>
      <td width="85%"><label for="user"></label>
      <input type="text" name="user" id="user"></td>
    </tr>
    <tr>
      <td>密码:</td>
      <td><label for="pw"></label>
      <input type="password" name="pw" id="pw"></td>
    </tr>
     <tr>
      <td>角色:</td>
      <td>
      <?php foreach($res as $val):?>
      <input type="radio" name="role" value="<?php echo $val['id']?>"/><?php echo $val['name']?>
      <?php endforeach;?>
	  </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" id="submit" value="提交"></td>
    </tr>
  </table>
</form>
<?php
$db = new mysql('localhost', 'root', '', 'cms', 'utf8');
if (isset($_POST['submit'])) {
    $user = $_POST['user'];
    $pw = $_POST['pw'];
    $data = array(
        'user' => $user,
        'pw' => $pw,
    );
    $add = $db->insert('user', $data);
    if (isset($_POST['role'])) {
        $role = $_POST['role'];
        $roleData = array(
            'u_id' => $add,
            'r_id' => $role
        );
        $roleAdd = $db->insert('user_role', $roleData);
    }
    if ($add) {
        echo '<script type="text/javascript">alert("操作成功");location=("list.php");</script>';
    }
}
?>
